<template>
  <div class="container">
    <div class="title">最新期刊内容列表</div>
    <div class="add-button">
      <el-button type="primary" @click="handleAdd">新增最新期刊</el-button>
    </div>
    <div class="table-container">
      <el-table :data="flowList" style="width: 100%">
        <el-table-column label="序号" width="60" type="index"> </el-table-column>
        <el-table-column prop="title" label="标题">
          <template slot-scope="scope">
            {{ scope.row.detail.title }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型">
          <template slot-scope="scope">
            {{ type[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="content" label="期刊介绍">
          <template slot-scope="scope">
            {{ scope.row.detail.content }}
          </template>
        </el-table-column>
        <el-table-column prop="index" label="排序" width="120"> </el-table-column>
        <el-table-column prop="status" label="是否展示" width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 1" type="success"> 展示中 </el-tag>
            <el-tag v-else type="info"> 未展示 </el-tag>
          </template>
        </el-table-column>
        <el-table-column width="200" label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button
              @click="handleEdit(scope.row)"
              v-permission="{ permission: '编辑内容', type: 'disabled' }"
              class="editBtn"
              >编辑</el-button
            >
            <el-popconfirm
              confirm-button-text="确认"
              cancel-button-text="取消"
              icon="el-icon-info"
              icon-color="red"
              title="这是一段内容确定删除吗？"
              @onConfirm="handleDelete(scope.row)"
              class="deletePop"
            >
              <el-button type="danger" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 编辑弹框 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="showDialog"
      width="800px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="resetForm"
    >
      <el-form ref="form" :model="temp" :rules="rules" label-width="98px">
        <el-form-item label="排序" prop="index">
          <el-input-number v-model="temp.index" :min="1" label="label"></el-input-number>
        </el-form-item>
        <el-form-item label="期刊内容" prop="art">
          <el-cascader v-model="temp.art" :options="options"></el-cascader>
        </el-form-item>
        <el-form-item label="有效状态">
          <el-switch v-model="temp.status" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="showDialog = false">取 消</el-button>
        <el-button type="primary" @click="confirmSave()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FlowModel from '../../model/flow'
import ContentModel from '../../model/content'

export default {
  name: 'list',
  data() {
    return {
      flowList: [],
      type: {
        100: '电影',
        200: '音乐',
        300: '句子',
      },
      showDialog: false,
      id: null,
      temp: {
        art: [],
        index: 1,
        status: 0,
      },
      delVisible: false,
      dialogTitle: '',
      options: [],
      rules: {
        art: [{ required: true, message: '内容介绍不能为空', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getFlowList()
  },
  methods: {
    async getFlowList() {
      this.flowList = await FlowModel.getFlowList()
    },
    async getContentList() {
      const contentList = await ContentModel.getContentList()
      this.options = this._generateOptionData(contentList)
      console.log(this.options)
    },
    _generateOptionData(data) {
      let types = []
      data.forEach(element => types.push(element.type))
      types = Array.from(new Set(types))
      // 往options中注入数据
      const options = types.map(el => ({
        value: el,
        label: this.type[el],
        children: [],
      }))
      options.forEach((item, index) => {
        const children = []
        data.forEach(d => {
          if (d.type === item.value) {
            children.push({
              value: d.id,
              label: d.title,
              disabled: !d.status,
            })
          }
        })
        options[index].children = children
      })
      return options
    },
    handleAdd() {
      this.dialogTitle = '新增内容'
      this.showDialog = true
      this.getContentList()
    },
    handleEdit(row) {
      // console.log(row)
      this.id = row.id
      this.temp = {
        index: row.index,
        status: row.status,
        art: [row.detail.type, row.detail.id],
      }
      this.dialogTitle = '编辑内容'
      this.showDialog = true

      this.getContentList()
    },
    async handleDelete(row) {
      console.log(row)
      const res = await FlowModel.delFlow(row)
      this.$message.success(res.message)
      await this.getFlowList()
    },
    async confirmSave() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.dialogTitle === '新增内容') {
            const res = await FlowModel.addFlow(this.temp.index, this.temp.art[0], this.temp.art[1], this.temp.status)
            this.$message.success(res.message)
          }
          if (this.dialogTitle === '编辑内容') {
            const res = await FlowModel.editFlow(
              this.id,
              this.temp.index,
              this.temp.art[0],
              this.temp.art[1],
              this.temp.status,
            )
            this.$message.success(res.message)
          }
          this.showDialog = false
          await this.getFlowList()
        }
      })
    },
    resetForm() {
      this.temp = {
        art: [],
        index: 1,
        status: 0,
      }
      this.id = null
      this.$refs.form.resetFields()
    },
  },
}
</script>

<style scoped lang="scss">
.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 20px;
    font-weight: 500;
    text-indent: 40px;
    border-bottom: 1px solid #dae1ec;
  }
  .add-button {
    padding: 20px 40px;
  }
  .table-container {
    padding: 0 40px;
  }
  .editBtn {
    margin-right: 8px;
  }
}
.deletePop {
  width: 100%;
  .el-popconfirm__main {
    margin-bottom: 8px;
  }
}
</style>
